@echo off

echo Testing database connection...

REM 创建一个简单的Java测试类来验证数据库连接
@echo import java.sql.Connection; > TestDBConnection.java
@echo import java.sql.DriverManager; >> TestDBConnection.java
@echo public class TestDBConnection { >> TestDBConnection.java
@echo     public static void main(String[] args) { >> TestDBConnection.java
@echo         String url = "jdbc:mysql://localhost:3306/chat_ai_system?useSSL=false&serverTimezone=UTC&allowPublicKeyRetrieval=true"; >> TestDBConnection.java
@echo         String username = "root"; >> TestDBConnection.java
@echo         String password = "root"; >> TestDBConnection.java
@echo         >> TestDBConnection.java
@echo         try { >> TestDBConnection.java
@echo             System.out.println("Attempting to connect to database..."); >> TestDBConnection.java
@echo             Connection conn = DriverManager.getConnection(url, username, password); >> TestDBConnection.java
@echo             System.out.println("SUCCESS! Database connection established."); >> TestDBConnection.java
@echo             conn.close(); >> TestDBConnection.java
@echo             System.out.println("\nDatabase configuration is correct. You can now start the backend service."); >> TestDBConnection.java
@echo         } catch (Exception e) { >> TestDBConnection.java
@echo             System.out.println("FAILED! Database connection error:"); >> TestDBConnection.java
@echo             e.printStackTrace(); >> TestDBConnection.java
@echo             System.out.println("\nPlease check:"); >> TestDBConnection.java
@echo             System.out.println("1. MySQL service is running"); >> TestDBConnection.java
@echo             System.out.println("2. Database 'chat_ai_system' exists"); >> TestDBConnection.java
@echo             System.out.println("3. Username and password are correct"); >> TestDBConnection.java
@echo         } >> TestDBConnection.java
@echo     } >> TestDBConnection.java
@echo } >> TestDBConnection.java

echo Compiling test class...
javac TestDBConnection.java

if exist TestDBConnection.class (
    echo Running database connection test...
    java TestDBConnection
    del TestDBConnection.class
    del TestDBConnection.java
) else (
    echo Java compiler not found. Please ensure JDK is installed.
    echo You can skip this test and proceed to start the backend service.
    del TestDBConnection.java
)

pause